/*
 *  Copyright (c) 2009, Hugo Augusto Alves
 *  All rights reserved.
 * 
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *        notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *      * Neither the name of the Hugo Augusto Alves nor the
 *        names of its contributors may be used to endorse or promote products
 *        derived from this software without specific prior written permission.
 * 
 *  THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.liteml.parser.elements;

import org.liteml.parser.LiteUtils.Target;

/**
 *
 * @author Hugo
 */
public class LiteStyledText extends LiteElement {

    private boolean bold = false;
    private boolean italic = false;
    private boolean underline = false;
    private boolean strike = false;
    private boolean superscript = false;
    private boolean subscript = false;
    private boolean preformatted = false;
    private String text;

    @Override
    public Type getType() {
        return Type.STYLED_TEXT;
    }

    @Override
    public String parse(Target t) {
        switch (t) {
            case HTML:
                int index = 0;
                StringBuilder sb = new StringBuilder();
                String res;
                if (preformatted) {
                    sb.insert(index, "<pre></pre>");
                    index += 5;
                } else {
                    if (bold) {
                        sb.insert(index, "<b></b>");
                        index += 3;
                    }
                    if (italic) {
                        sb.insert(index, "<i></i>");
                        index += 3;
                    }
                    if (underline) {
                        sb.insert(index, "<u></u>");
                        index += 3;
                    }
                    if (strike) {
                        sb.insert(index, "<del></del>");
                        index += 5;
                    }
                    if (superscript) {
                        sb.insert(index, "<sup></sup>");
                        index += 5;
                    }
                    if (subscript) {
                        sb.insert(index, "<sub></sub>");
                        index += 5;
                    }
                }

                // special chars
                res = sb.insert(index, text).toString();
                res = res.replace("- ", "&#8212; ");
                res = res.replace("(c)", "&copy;");
                res = res.replace("(r)", "&reg;");
                res = res.replace("(tm)", "&#8482;");
                res = res.replace(">>", "&raquo;");
                res = res.replace("<<", "&laquo;");
                res = res.replace("\n", "<br />");


                return res;
            case PLAIN:
            default:
                return "";
        }
    }

    public LiteStyledText() {
    }

    public LiteStyledText(String text) {
        this.text = text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setBold(boolean bold) {
        this.bold = bold;
    }

    public void setItalic(boolean italic) {
        this.italic = italic;
    }

    public void setPreformatted(boolean preformatted) {
        this.preformatted = preformatted;
    }

    public void setStrike(boolean strike) {
        this.strike = strike;
    }

    public void setSubscript(boolean subscript) {
        this.subscript = subscript;
    }

    public void setSuperscript(boolean superscript) {
        this.superscript = superscript;
    }

    public void setUnderline(boolean underline) {
        this.underline = underline;
    }

    public boolean isBold() {
        return bold;
    }

    public boolean isItalic() {
        return italic;
    }

    public boolean isPreformatted() {
        return preformatted;
    }

    public boolean isStrike() {
        return strike;
    }

    public boolean isSubscript() {
        return subscript;
    }

    public boolean isSuperscript() {
        return superscript;
    }

    public String getText() {
        return text;
    }

    public boolean isUnderline() {
        return underline;
    }



    public void copyStyles(LiteStyledText lst) {
        if(lst.isBold()) this.bold = true;
        if(lst.isItalic()) this.italic = true;
        if(lst.isUnderline()) this.underline = true;
        if(lst.isStrike()) this.strike = true;
        if(lst.isSuperscript()) this.superscript = true;
        if(lst.isSubscript()) this.subscript = true;
    }

}
